package cn.edu.cdut.service;

import cn.edu.cdut.dao.DownloadFileInfoDao;
import cn.edu.cdut.dao.PlanCourseInfoDao;
import cn.edu.cdut.dao.ResultInfoDao;
import cn.edu.cdut.dao.StudentInfoDao;
import cn.edu.cdut.dto.MyRes;
import cn.edu.cdut.model.DownloadFileInfo;
import cn.edu.cdut.model.PlanCourseInfo;
import cn.edu.cdut.model.ResultInfo;
import cn.edu.cdut.model.StudentInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

/**
 * Author  kobe
 * Created on 2017-06-12-10:56
 */
@Service
public class DownloadService {

        @Autowired
        private DownloadFileInfoDao downloadFileInfoDao;

        //获取所有下载文件信息
        public MyRes getAllDownloadFileInfo() {
                Iterable<DownloadFileInfo> downloadFileInfos = downloadFileInfoDao.findAll();
                //转换为Json字符串
                ObjectMapper mapper = new ObjectMapper();
                Iterator iter = downloadFileInfos.iterator();
                String[] resArr = new String[(int)downloadFileInfoDao.count()];
                int i = 0;
                while(iter.hasNext()){
                        try {
                                resArr[i] = mapper.writeValueAsString(iter.next());
                                i++;
                        } catch (JsonProcessingException e) {
                                e.printStackTrace();
                                return new MyRes(201, "获取下载文件信息失败！", null);
                        }
                }
                return new MyRes(200, null, resArr);
        }

        ////下载文件
        //public File downloadFile(String fileName) throws FileNotFoundException {
        //        return ResourceUtils.getFile("D:/MY-IDEA-Project/sds02/src/main/resources/static/" + fileName);
        //}
}
